package com.antler.smt.admin.utils;

import com.antler.smt.utils.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Redis工具类
 *
 * @author vic
 * @date 2017-09-20 17:12
 */
@Component
public class RedisUtil {
    /**
     * redis模板
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 批量删除对应的value
     *
     * @param keys 可变参数，键
     */
    public void remove(final String... keys) {
        RedisCacheUtil.remove(keys);
    }

    /**
     * 批量删除key
     *
     * @param pattern 字符串键
     */
    public void removePattern(final String pattern) {
        RedisCacheUtil.remove(redisTemplate, pattern);
    }

    /**
     * 删除对应的value
     *
     * @param key 键
     */
    public void remove(final String key) {
        RedisCacheUtil.remove(redisTemplate, key);
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key 键
     * @return boolean值
     */
    public boolean exists(final String key) {
        return RedisCacheUtil.exists(redisTemplate, key);
    }

    /**
     * 读取缓存
     *
     * @param key 键
     * @return 泛型对象
     */
    public <T> T get(final String key) {
        return RedisCacheUtil.get(redisTemplate, key);
    }

    /**
     * 写入缓存
     *
     * @param key   键
     * @param value 值
     * @return boolean值
     */
    public boolean set(final String key, Object value) {
        return RedisCacheUtil.set(redisTemplate, key, value);
    }

    /**
     * 写入缓存
     *
     * @param key        键
     * @param value      值
     * @param expireTime 过期时间（秒）
     * @return boolean值
     */
    public boolean set(final String key, Object value, Long expireTime) {
        return RedisCacheUtil.set(redisTemplate, key, value, expireTime);
    }
}
